package org.c797797ty.railway.service;
import org.c797797ty.railway.model.User;
import org.c797797ty.railway.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author C797797ty
 * @data 2025/11/3 14:16
 */

public class UserService {
    private static final Scanner scanner = new Scanner(System.in);

    // 用户注册
    public static User register() {
        System.out.print("新用户名: ");
        String username = scanner.nextLine();
        System.out.print("新密码: ");
        String password = scanner.nextLine();

        try {
            // 检查用户名是否存在
            ResultSet rs = DBUtil.executeQuery(
                    "SELECT COUNT(*) FROM users WHERE username = ?", username);
            rs.next();
            if (rs.getInt(1) > 0) {
                System.out.println("用户名已存在！");
                return null;
            }

            // 插入新用户
            DBUtil.executeUpdate(
                    "INSERT INTO users (username, password) VALUES (?, ?)", username, password);

            // 获取用户ID
            rs = DBUtil.executeQuery("SELECT LAST_INSERT_ID()");
            rs.next();
            User user = new User(rs.getInt(1), username, password);
            System.out.println("注册成功！");
            return user;
        } catch (SQLException e) {
            System.err.println("注册失败: " + e.getMessage());
            return null;
        }
    }

    // 用户登录
    public static User login() {
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        try (ResultSet rs = DBUtil.executeQuery(
                "SELECT * FROM users WHERE username = ? AND password = ?", username, password)) {

            if (rs.next()) {
                User user = new User(rs.getInt("id"), username, password);
                user.setLoggedIn(true);
                System.out.println("登录成功！");
                return user;
            }
            System.out.println("用户名或密码错误！");
            return null;
        } catch (SQLException e) {
            System.err.println("登录失败: " + e.getMessage());
            return null;
        }
    }

    // 用户退出
    public static void logout(User user) {
        if (user != null) {
            user.setLoggedIn(false);
            try {
                DBUtil.executeUpdate(
                        "UPDATE users SET is_logged_in = FALSE WHERE id = ?", user.getId());
            } catch (SQLException e) {
                System.err.println("更新状态失败: " + e.getMessage());
            }
        }
        System.out.println("已退出登录");
    }



    /**
     * 根据用户名查询用户
     */
    public static User getUserByUsername(String username) {
        try (ResultSet rs = DBUtil.executeQuery(
                "SELECT * FROM users WHERE username = ?",
                username)) {

            if (rs.next()) {
                User user = new User(
                        rs.getInt("id"),
                        rs.getString("username"),
                        rs.getString("password")
                );
                return user;
            }
        } catch (SQLException e) {
            System.err.println("查询用户失败: " + e.getMessage());
        }
        return null;
    }








}